package cn.xiaomubiao.oschine;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;
import cn.xiaomubiao.oschine.activity.MyInfoBaseActivity;
import cn.xiaomubiao.oschine.contants.Host;
import cn.xiaomubiao.oschine.contants.MainTab;
import cn.xiaomubiao.oschine.fragment.IpSettingFragment;
import cn.xiaomubiao.oschine.fragment.LeftDrawerFragment;
import cn.xiaomubiao.oschine.util.SpUtils;
import cn.xiaomubiao.oschine.util.ToastUtils;
import cn.xiaomubiao.oschine.ui.dialog.DialogView;

/**
 * 主界面 主结构
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // 控件对象
    @BindView(R.id.toolbar_main)
    Toolbar mToolBar;
    @BindView(R.id.root_content_main)
    FrameLayout mRootContent;
    @BindView(R.id.menu_left_main)
    FrameLayout mMenuLeft;
    @BindView(R.id.drawer_main)
    DrawerLayout mDrawer;
    @BindView(R.id.tabhost_main)
    FragmentTabHost mTabhost;

    private Dialog dialog; //对话框对象
    private LinearLayout dialogLayoutView; //对话框布局对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置主机名和端口号
        String ip = SpUtils.getString(this, "ip", "10.0.2.2");
        String port = SpUtils.getString(this, "port", "8080");

        Host.HOST_DEFAULT = "http://"+ip+":"+port+"/oschina/";

        ToastUtils.showToastShort(this,"当前主机名为: "+Host.HOST_DEFAULT);

        // 主界面视图
        initView();
        // 初始化对话框视图
        initDialogView();
        // 初始化对话框中的点击事件
        initDialogEvent();
        // 初始化侧边栏的布局
        initDrawerView();



    }

    private void initDrawerView() {
        // 获取fragment管理者
        FragmentManager manager = getSupportFragmentManager();
        // 获取事务对象
        FragmentTransaction transaction = manager.beginTransaction();
        // 通过事务对象进行fragment的添加
        transaction.replace(R.id.menu_left_main,new LeftDrawerFragment());

        // 进行提交
        transaction.commit();

    }

    private void initDialogEvent() {

        // 先找到里面的控件，并设置点击监听
        dialogLayoutView.findViewById(R.id.tv_quick_text).setOnClickListener(this);
        dialogLayoutView.findViewById(R.id.tv_quick_phone).setOnClickListener(this);
        dialogLayoutView.findViewById(R.id.tv_quick_album).setOnClickListener(this);
        dialogLayoutView.findViewById(R.id.ib_quick).setOnClickListener(this);

    }

    private void initDialogView() {
        // 设置自定义布局的对话框视图
        // 构建一个对话框视图类对象
        DialogView dialogView = new DialogView(this);
        // 传入布局文件，返回对话框对象
        dialog = dialogView.getDialogView(R.layout.view_bommom_dialog);
        // 获取布局文件所对应的view对象，用于找到里面控件
        dialogLayoutView = (LinearLayout) dialogView.getLayoutView();

    }

    private void initView() {
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        syncToolBarAndDrawer();
        initTabHost();

    }

    /**
     * 初始化FragmentTabHost
     */
    private void initTabHost() {
        mTabhost.setup(this, getSupportFragmentManager(), R.id.root_content_main);

        //获取枚举数组
        MainTab[] tabs = MainTab.values();

        for (int i = 0; i < tabs.length; i++) {
            //打气TabView
            View view = View.inflate(this, R.layout.view_tab, null);
            //获取TabView中的TextView
            TextView tvTab = (TextView) view.findViewById(R.id.tv_tab);
            tvTab.setText(tabs[i].getTitle());

            //获取图标ID的Drawable对象
            Drawable drawable = this.getResources().getDrawable(
                    tabs[i].getResId());
            //设置TextView上下左右的绘图
            tvTab.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                    null);

            if (i == 2) {
                view.setVisibility(View.INVISIBLE);
                view.setClickable(false);
            }

            //创建Tab信息，并为他设置View
            TabHost.TabSpec spec = mTabhost.newTabSpec(tabs[i].getTag()).setIndicator(view);
            //去除分割线
            TabWidget tabWidget = mTabhost.getTabWidget();
            tabWidget.setDividerDrawable(null);
            //添加Tab
            mTabhost.addTab(spec, tabs[i].getClazz(), null);
        }
    }

    /**
     * 同步ToolBar与DrawerLayout
     */
    private void syncToolBarAndDrawer() {
        setSupportActionBar(mToolBar);
        mToolBar.inflateMenu(R.menu.main_activity_menu);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawer, mToolBar, R.string.open, R.string.close);
        toggle.syncState();
//        mDrawer.setDrawerListener(toggle); //已过时
        mDrawer.addDrawerListener(toggle);

    }


    //添加菜单按钮
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //装配菜单按钮
        getMenuInflater().inflate(R.menu.search_menu, menu);
        return super.onCreateOptionsMenu(menu);

    }

    //监听菜单按钮
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search_content:
                Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }


    //中心按钮点击事件
    @OnClick(R.id.iv_quick)
    public void quickOnClick(View v) {
        // 点击弹出对话框
        dialog.show();

    }


    //中心按钮长按事件
    @OnLongClick(R.id.iv_quick)
    public boolean quickOnLongClick(View v) {
        Intent intent = new Intent(this, MyInfoBaseActivity.class);
        intent.putExtra("title","开源中国");
        intent.putExtra("className", IpSettingFragment.class.getName());
        startActivity(intent);
        return true;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.tv_quick_text:
                ToastUtils.showToastShort(this,"文字");

                break;
            case R.id.tv_quick_phone:
                ToastUtils.showToastShort(this,"相册");

                break;
            case R.id.tv_quick_album:
                ToastUtils.showToastShort(this,"拍照");

                break;
            case R.id.ib_quick:
                // 关闭对话框
                dialog.dismiss();

                break;
        }

    }
}